<?php

/**
 * The initial settings and main system.
 * 
 * @author Cesar Augustus Silva <cesaraugustussilva@linuxmail.org>
 * @copyright Copyright (c) 2013, Cesar Augustus Silva
 * @license http://www.gnu.org/licenses/gpl.html GNU GPL v3.0
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <­http://www.gnu.org/licenses/>.
 */

/* On: Debug only */
@ini_set('display_errors', 'Off');

/* Improve PHP configuration to prevent issues */
ini_set('upload_max_filesize', '100M');
ini_set('magic_quotes_runtime', 0);
ini_set('zlib.output_compression', 'on');

/* Limit execution time of the script */
ini_set('max_execution_time', '0');
set_time_limit(0);

// correct Apache charset (except if it's too late
if (!headers_sent())
    header('Content-Type: text/html; charset=utf-8');

if (!file_exists(dirname(__FILE__).'/settings.inc.php'))
    die('Erro: Arquivo \'settings.inc.php\' não existe!');
else
    require_once(dirname(__FILE__).'/settings.inc.php');

/* Include all defines */
if (!file_exists(dirname(__FILE__).'/defines.inc.php'))
    die('Erro: Arquivo \'defines.inc.php\' não existe!');
else
    require_once(dirname(__FILE__).'/defines.inc.php');

/* Include all defines of errors */
if (!file_exists(dirname(__FILE__).'/errors.inc.php'))
    die('Erro: Arquivo \'errors.inc.php\' não existe!');
else
    require_once(dirname(__FILE__).'/errors.inc.php');

/* Autoload */
require_once(dirname(__FILE__).'/autoload.php');

/* Redefine REQUEST_URI if empty (on some webservers...) */
if (!isset($_SERVER['REQUEST_URI']) OR empty($_SERVER['REQUEST_URI'])) {
    if (substr($_SERVER['SCRIPT_NAME'], -9) == 'index.php' && empty($_SERVER['QUERY_STRING']))
        $_SERVER['REQUEST_URI'] = dirname($_SERVER['SCRIPT_NAME']).'/';
    else {
        $_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'];
        if (isset($_SERVER['QUERY_STRING']) AND !empty($_SERVER['QUERY_STRING']))
            $_SERVER['REQUEST_URI'] .= '?'.$_SERVER['QUERY_STRING'];
    }
}

/* Trying to redefine HTTP_HOST if empty (on some webservers...) */
if (!isset($_SERVER['HTTP_HOST']) OR empty($_SERVER['HTTP_HOST']))
    $_SERVER['HTTP_HOST'] = @getenv('HTTP_HOST');